package freemarker.template;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import freemarker.core.Environment;

public class TemplateEx extends Template{

	  public TemplateEx(String name, Reader reader, Configuration cfg, String encoding) throws IOException {
		  super(name, reader, cfg, encoding);
	  }

	public Environment createProcessingEnvironment(Object rootMap, Writer out, ObjectWrapper wrapper) throws TemplateException, IOException {
		TemplateHashModel root = null;
		if (rootMap instanceof TemplateHashModel) {
			root = (TemplateHashModel) rootMap;
		} else {
			if (wrapper == null) {
				wrapper = getObjectWrapper();
			}

			try {
				root = rootMap != null ? (TemplateHashModel) wrapper
						.wrap(rootMap) : new SimpleHash(wrapper);
				if (root == null) {
					throw new IllegalArgumentException(wrapper.getClass()
							.getName()
							+ " converted "
							+ rootMap.getClass().getName() + " to null.");
				}
			} catch (ClassCastException e) {
				throw new IllegalArgumentException(wrapper.getClass().getName()
						+ " could not convert " + rootMap.getClass().getName()
						+ " to a TemplateHashModel.");
			}
		}
		return new Environment(this, root, out);
	}
}
